package com.zh.tea.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zh.tea.entity.Feedback;
import com.zh.tea.service.FeedbackService;
import com.zh.tea.service.impl.FeedbackServiceImpl;

/**
 * Servlet implementation class FeedbackServlet
 */
@WebServlet("/FeedbackServlet")
public class FeedbackServlet extends HttpServlet {
	
	FeedbackService fs = new FeedbackServiceImpl();
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FeedbackServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String opt = request.getParameter("opt");
		if("sub".equals(opt)) {
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			String content = request.getParameter("content");
			Feedback fb = new Feedback();
			fb.setId(null);
			fb.setName(name);
			fb.setPhone(phone);
			fb.setContent(content);
			int a = fs.add(fb);
			if(a>0) {
				out.print("<script>alert('提交成功!');location.href='contactus.jsp'</script>");
			}else {
				out.print("<script>alert('提交失败!请重新提交');location.href='contactus.jsp'</script>");
			}
		}
		if("del".equals(opt)) {
			String fid = request.getParameter("id");
			int id = Integer.parseInt(fid);
			int a = fs.del(id);
			if(a>0) {
				out.print("<script>alert('删除成功!');location.href='feedback.jsp'</script>");
			}else {
				out.print("<script>alert('删除失败!');location.href='feedback.jsp'</script>");
			}
					
		}
		if("selAll".equals(opt)) {
			List<Feedback> list = fs.selAll();
			session.setAttribute("list", list);
			response.sendRedirect("feedback.jsp");
		}
	}

}
